import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import './assets/global.css'
import axios from 'axios'
import moment from 'moment'
import md5 from 'js-md5';
import 'babel-polyfill'
import Promise from 'es6-promise'

Promise.polyfill();

// import Vuex from 'vuex'
// import store from './vuex/store'

// Vue.use(Vuex)
Vue.use(require('vue-moment'));
Vue.prototype.moment = moment
Vue.prototype.$http = axios
axios.defaults.baseURL = "http://localhost:8888/apartment/";
Vue.prototype.$md5 = md5;
// 引用路由插件
// import VueRouter from 'vue-router'
//引入路由配置文件
import router from './router'
// // 试用路由插件
// Vue.use(VueRouter)
// const router = new VueRouter(routes)
Vue.use(ElementUI)

Vue.config.productionTip = false

//自动给所有请求添加请求头
axios.interceptors.request.use(function(config){
    let token=localStorage.getItem('token');
    if(token){
        config.headers['Authorization']=token;
    }
    return config;
},error => {
    return Promise.reject(error);
})

//响应回的token是否过期
axios.interceptors.response.use(res=>{
    console.log('响应回来'+res.data.code)
    if(res.code==800){
        ElementUI.Message({
            message:'身份已失效',
            type:'err'
        });
    //    清空token
        localStorage.removeItem('token')
    //    跳转
        this.$router.push({path:'/login'})
    }else{
        return res
    }
},error => {
    return Promise.reject(error);
})

//时间过滤器--修改时间的格式

Vue.filter('FormatDate',function(dateStr,pattern='YYYY-MM-DD'){
    return moment(dateStr).format(pattern);
})

new Vue({
  router,
  render: h => h(App),
  template: '<App/>'
}).$mount('#app')
